package cn.detu.coolshark.filter;

import cn.detu.coolshark.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "MyFilter",urlPatterns = {"/insertProduct.html","/admin.html"})
public class MyFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest st = (HttpServletRequest) req;
        HttpServletResponse se = (HttpServletResponse)resp;
        HttpSession session = st.getSession();
        User user =(User)session.getAttribute("user");
        if (user!=null){
            chain.doFilter(req,resp);
        }else {
            se.sendRedirect("/login.html");
        }

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
